package com.cxd.ancun.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.cxd.ancun.constants.LoanConstants.RepayStatus;
import com.cxd.ancun.entity.base.LoanRepay;
import com.cxd.ancun.entity.base.RepayRoadmap;
import com.cxd.base.util.ArithUtil;
import com.cxd.base.util.BeanTools;
/**
 * <一句话功能简述> <功能详细描述>
 * @author wtj
 * @date 2016年4月11日 下午2:56:53
 * @company CXD
 * @version [Copyright (c) 2016 V001]
 */
@Service("loanService")
public class LoanService {
	
//	private static final Logger logger = Logger.getLogger(LoanService.class);
	@Resource
	public BeanTools db;
	/**
	 * 还款路标（待还、已还金额之类）
	 */
	private RepayRoadmap repayRoadmap;
	public RepayRoadmap getRepayRoadmap(String loanId){
		List<LoanRepay> loanRepays = this.getLoanRepayByLoanId(loanId);
		if (loanRepays!=null && loanRepays.size()>0) {
			// 未还本金
			Double unPaidCorpus = 0D;
			// 未还利息
			Double unPaidInterest = 0D;
			// 未还手续费
			Double unPaidFee = 0D;
			// 未还罚息
			Double unPaidDefaultInterest = 0D;
			// 已还本金
			Double paidCorpus = 0D;
			// 已还利息
			Double paidInterest = 0D;
			// 已还手续费
			Double paidFee = 0D;
			// 已还罚息
			Double paidDefaultInterest = 0D;
			// 下个还款日
			Date nextRepayDate = null;
			// 下个还款本金
			Double nextRepayCorpus = null;
			// 下个还款利息
			Double nextRepayInterest = null;
			// 下个还款手续费
			Double nextRepayFee = null;
			// 下个还款罚息
			Double nextRepayDefaultInterest = null;
			// 已还期数
			int paidPeriod = 0;
			for (int i = 0; i < loanRepays.size(); i++) {
				LoanRepay ir = loanRepays.get(i);
				if (ir.getStatus().equals(RepayStatus.BAD_DEBT)
						|| ir.getStatus().equals(RepayStatus.OVERDUE)
						|| ir.getStatus().equals(RepayStatus.REPAYING)) {
					unPaidCorpus = ArithUtil.add(unPaidCorpus, ir.getCorpus());
					unPaidInterest = ArithUtil.add(unPaidInterest,
							ir.getInterest());
					unPaidFee = ArithUtil.add(unPaidFee, ir.getFee());
					unPaidDefaultInterest = ArithUtil.add(
							unPaidDefaultInterest, ir.getDefaultInterest());
					if (i == 0
							||loanRepays.get(i - 1).getStatus()
									.equals(RepayStatus.COMPLETE)) {
						// 下一期待还款
						nextRepayDate = ir.getRepayDay();
						nextRepayCorpus = ir.getCorpus();
						nextRepayInterest = ir.getInterest();
						nextRepayFee = ir.getFee();
						nextRepayDefaultInterest = ir.getDefaultInterest();
					}
				} else if (ir.getStatus().equals(RepayStatus.COMPLETE)) {
					paidCorpus = ArithUtil.add(paidCorpus, ir.getCorpus());
					paidInterest = ArithUtil
							.add(paidInterest, ir.getInterest());
					paidFee = ArithUtil.add(paidFee, ir.getFee());
					paidDefaultInterest = ArithUtil.add(paidDefaultInterest,
							ir.getDefaultInterest());
					paidPeriod++;
				}
			}

			this.repayRoadmap = new RepayRoadmap(unPaidCorpus, unPaidInterest,
					unPaidFee, unPaidDefaultInterest, paidCorpus, paidInterest,
					paidFee, paidDefaultInterest, nextRepayDate,
					nextRepayCorpus, nextRepayInterest, nextRepayFee,
					nextRepayDefaultInterest, paidPeriod, loanRepays
							.size() - paidPeriod, true);
		}
		return this.repayRoadmap;
	}
	
	/**
	 * 获取借款的还款计划
	 * @author wtj
	 * @date 2016年4月11日下午1:54:01
	 * @param loanId
	 * @return
	 */
	public List<LoanRepay> getLoanRepayByLoanId(String loanId){
		List<LoanRepay> liBean = db.queryListBean("LOAN_REPAY", new String[]{"loan_id"},  new String[]{loanId}, LoanRepay.class);
		return liBean;
	}
}
